/**
 * 尊敬的用户，你好：页面 JS 面板是高阶用法，一般不建议普通用户使用，如需使用，请确定你具备研发背景，能够自我排查问题。
 * 我们可以用 JS 面板来开发一些定制度高功能，比如：调用API接口，自定义动作执行等等。
 */

// 当页面渲染完毕后马上调用下面的函数，这个函数是在当前页面 - 设置 - 生命周期 - 页面加载完成时中被关联的。
export function didMount() {
    ("「页面 JS」：当前页面地址 " + location.href);
}


export function cardGenerated() {
    const self = this;
    let regionCode = self.$$("tree_single_select_6rwcpk").selectedData[0].data.columns.CODE;
    let lawEnforcementCode = self.$$('single_select_867a32').cValue.padStart(2, '0');
    let randomNum = '';

    getCardNumber(regionCode, lawEnforcementCode).then(result => {
        console.log(result);
        self.$$("text_input_d4m6ot").cValue = result;
        randomNum = String(result).padStart(4, '0');
        let cardNo = `${regionCode}${lawEnforcementCode}${randomNum}`;
        self.$$('text_input_fr4lav').cValue = cardNo;
    });
    // region_code manage_unit_code   查询 current_sequence


}


function getCardNumber(regionCode, lawEnforcementCode) {
    return new Promise(function (resolve, reject) {
        // let law_confiscated_card_number_index = 'ds_tbl:t_50c0a039d1274a2b83d2ba0029107055';
        api.EXEC_FORMULA(`ENTITY_SELECT_ONE('ds_tbl:t_50c0a039d1274a2b83d2ba0029107055', 'current_sequence', QUERY_AND(QUERY_EQ('region_code','${regionCode}'), QUERY_EQ('manage_unit_code','${lawEnforcementCode}')))`).then(function (res) {
            debugger
            let currentSequence = 0;
            if (res === null || res === '') {
                currentSequence = 1;
                insertLawConfiscatedCardInfo(regionCode, lawEnforcementCode, currentSequence)
            } else {
                currentSequence = Number(res.result) + 1;
                updateLawConfiscatedCardInfo(regionCode, lawEnforcementCode, currentSequence);
            }
            resolve(currentSequence);
        });
    })
}

function updateLawConfiscatedCardInfo(regionCode, lawEnforcementCode, currentSequence) {

    return api.EXEC_FORMULA(
        "UPDATE('ds_tbl:t_50c0a039d1274a2b83d2ba0029107055', QUERY_AND(QUERY_EQ('region_code','" + regionCode + "'), QUERY_EQ('manage_unit_code','" + lawEnforcementCode + "')), '', ['current_sequence', '" + currentSequence + "'])"
    ).then(function (updateRes) {
        console.log('Update result:', updateRes);

    });
}



function insertLawConfiscatedCardInfo(regionCode, lawEnforcementCode, currentSequence) {
    const insertApprovalSql = "INSERT('ds_tbl:t_50c0a039d1274a2b83d2ba0029107055',['region_code','" + regionCode + "','manage_unit_code','" + lawEnforcementCode + "','current_sequence','" + currentSequence + "','create_time',NOW()])";
    api.EXEC_FORMULA(insertApprovalSql).then(res => {
        console.log(res)
    })
}


/**
 * 申请部门 值改变
 */
export function unitOnChange() {
    const self = this;
    let unitIdEle = self.$$("tree_single_select_8ognhw");
    let unitNameEle = self.$$("text_input_fkv13");
    let unitText = unitIdEle.textData.props.defaultValue.value;
    let unitNmae = getLastDepartment(unitText);
    unitNameEle.cValue = unitNmae;
}


function getLastDepartment(unitText) {
    const parts = unitText.split('/');
    return parts[parts.length - 1];
}
